$(function () {
    var markid = getUrlString('markid');
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0;
    let sort;
    if (getCookie('sort')) {
        sort = getCookie('sort')
    }else {
        sort = 0
    }
    //跳往全部
    $('.informationAll').click(function () {
        window.location.href = 'myEggPool.html?mainstate=1&type=1&markid='+markid
    });
    //跳往预售
    $('.advanceNoticeBtn').click(function () {
        window.location.href = 'myEggPoolPresell.html?mainstate=1&type=1&markid='+markid
    });
    //跳往咸蛋
    $('.pullEggBtn').click(function () {
        window.location.href = 'myEggPoolSaltedEgg.html?mainstate=2&type=0&markid='+markid
    });



    // $(document).on('click', '.sort', function() {
    $('.sort').click(function () {
        var buttons1 = [
            {
                text: '到期时间从远到近',
                bold: true,
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/up.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 10;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '到期时间从近到远',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 11;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '入库时间从近到远',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 0;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '入库时间从远到近',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 1;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '请选择排列方式',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#cashCommodityList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                cashCommodityList(page,sort);

            }
        }
    });

    function cashCommodityList (page,sort) {
        var data = {
            'markid':markid,
            'mainstate':1,
            'type':0,
            'sort':sort,
            'page':page
        };
        getData('post','/niu/apiv3/niu_getuserkuv3.php',data,function (data) {
            console.log(data);
            if(data.resultcode == 0 ){
                var append = '';
                var dateEnd = new Date();//获取当前时间
                var datetime=dateEnd.getFullYear().toString()  + (zeroFill(dateEnd.getMonth() + 1)).toString() + zeroFill(dateEnd.getDate()).toString() +
                    zeroFill(dateEnd.getHours()).toString()  + zeroFill(dateEnd.getMinutes()).toString()  + zeroFill(dateEnd.getSeconds()).toString();
                var nextDay = dateEnd.getFullYear().toString()  + (zeroFill(dateEnd.getMonth() + 1)).toString() + zeroFill(dateEnd.getDate()+1).toString() +'010000';
                var outtime;
                //判断小时数
                var ifHour = getYMDHMS(datetime,nextDay).substring(3,4);
                if(ifHour === '小'){
                    outtime = getYMDHMS(datetime,nextDay).substring(2,3);
                }else {
                    outtime = getYMDHMS(datetime,nextDay).substring(2,4);
                }
                $(data.data).each(function () {
                    append += '<li>' +
                        '           <input type="hidden" class="id" value="'+this.id+'">'+
                        '           <input type="hidden" class="poolid" value="'+this.niupoolid+'">'+
                        '           <input type="hidden" class="isLockon" value="'+this.lockon+'">'+
                        '<input type="hidden" class="bukuan_status" value="'+this.bukuan_status+'">'+
                        '<input type="hidden" class="is_bukuan" value="'+this.is_bukuan+'">'+
                        '                    <div class="pic">' ;
                        append += '                        <img src="'+this.pics+'-toysbox11'+'" alt="">' ;
                    if(this.lockon === '1'){
                        append += '                        <img style="position:absolute;top: 0;left: 0" src="../../res/img/my/myEggPool/sd.png" alt="已锁库">' ;
                    }

                    if(this.outtime === '0天'){
                        append += '                        <div class="dayNum">'+outtime+' h</div>' ;
                    }else {
                        append += '                        <div class="dayNum">'+this.outtime+'</div>' ;
                    }
                    append+= '                   </div>' ;
                    if (this.bukuan_status === '0'){
                        append += '                    <div class="name"><img src="../../res/img/dj2.png" alt=""><span>'+this.name+'</div>' ;
                    }else {
                        append += '                    <div class="name"><span>'+this.name+'</span></div>' ;
                    }

                    append +='                </li>'

                });
                if(data.pageid == 0){
                    $('#cashCommodityList').empty().append(append);
                }else {
                    $('#cashCommodityList').append(append);
                }

                //总条数
                totalSize = totalSize + data.pagesize;
                // alert(totalSize > data.datacount);
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        })
    }
    $("#cashCommodityList li").live("click", function(){
        var id = $(this).find('.id').val();
        var poolid = $(this).find('.poolid').val();
        var isLockon = $(this).find('.isLockon').val();
        var bukuan_status = $(this).find('.bukuan_status').val();
        var is_bukuan = $(this).find('.is_bukuan').val();
        var title = $(this).children('.name').find('span').html();
        cashCommodityListDetail(id,poolid,title,isLockon,bukuan_status,is_bukuan)
    });
    function cashCommodityListDetail(id,poolid,title,isLockon,bukuan_status,is_bukuan) {
        let buttons1;
        if (bukuan_status === '0' && is_bukuan === '0'){
            if(isLockon === '0'){
                buttons1 = [
                    {
                        text: '查看详情',
                        bold: true,
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id;
                        }
                    },
                    {
                        text: '商品锁库',
                        onClick: function () {
                            window.location.href="dancool://web="+urlView+"my/myEggPoolLockKu.html?type=1";
                        }
                    },
                    {
                        text: '挂卖咸蛋',
                        onClick: function () {
                            window.location.href="dancool://fullweb="+urlView+"my/myEggPoolAddSaltedEgg.html?id="+id;
                        }
                    },
                    {
                        text: '系统回购',
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id+'&isBuyBack=1';
                        }
                    },
                    {
                        text: '补款',
                        onClick: function () {
                            window.location.href = 'dancool://fullweb='+urlView+'HPStore/HPstoreDetailBK.html?kutoysid='+id+'&poolid='+poolid
                        }
                    },
                    {
                        text: '返回',
                        onClick: function () {

                        }
                    }];
            }else {
                buttons1 = [
                    {
                        text: '查看详情',
                        bold: true,
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id+'&isLockon='+isLockon;
                        }
                    },
                    {
                        text: '商品解锁',
                        onClick: function () {
                            // window.location.href="dancool://web="+urlView+"my/myEggPoolLockKu.html?type=0&";
                            var data = {
                                'markid':markid,
                                'set':'unlock',
                                'kutoysids':id
                            };
                            getData('post','/niu/apiv3/niu_dolockon.php',data,function (data) {
                                console.log(data);
                                if(data.resultcode === "0" ){
                                    $.toast('解锁成功');
                                    setTimeout(function () {
                                        window.location.reload();
                                    },500)
                                }else {
                                    $.toast(data.msg)
                                }
                            })
                        }
                    },
                    {
                        text: '补款',
                        onClick: function () {
                            window.location.href = 'dancool://fullweb='+urlView+'HPStore/HPstoreDetailBK.html?kutoysid='+id+'&poolid='+poolid
                        }
                    },
                    {
                        text: '返回',
                        onClick: function () {

                        }
                    }];
            }
        }else {
            if(isLockon === '0'){
                buttons1 = [
                    {
                        text: '查看详情',
                        bold: true,
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id;
                        }
                    },
                    {
                        text: '商品锁库',
                        onClick: function () {
                            window.location.href="dancool://web="+urlView+"my/myEggPoolLockKu.html?type=1";
                        }
                    },
                    {
                        text: '挂卖咸蛋',
                        onClick: function () {
                            window.location.href="dancool://fullweb="+urlView+"my/myEggPoolAddSaltedEgg.html?id="+id;
                        }
                    },
                    {
                        text: '系统回购',
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id+'&isBuyBack=1';
                        }
                    },
                    {
                        text: '返回',
                        onClick: function () {

                        }
                    }];
            }else {
                buttons1 = [
                    {
                        text: '查看详情',
                        bold: true,
                        onClick: function () {
                            window.location.href = "dancool://fullweb="+urlView+"my/myEggPoolDetail.html?type=0&kuid="+id+'&isLockon='+isLockon;
                        }
                    },
                    {
                        text: '商品解锁',
                        onClick: function () {
                            // window.location.href="dancool://web="+urlView+"my/myEggPoolLockKu.html?type=0&";
                            var data = {
                                'markid':markid,
                                'set':'unlock',
                                'kutoysids':id
                            };
                            getData('post','/niu/apiv3/niu_dolockon.php',data,function (data) {
                                console.log(data);
                                if(data.resultcode === "0" ){
                                    $.toast('解锁成功');
                                    setTimeout(function () {
                                        window.location.reload();
                                    },500)
                                }else {
                                    $.toast(data.msg)
                                }
                            })
                        }
                    },
                    {
                        text: '返回',
                        onClick: function () {

                        }
                    }];
            }
        }


        $.modal({
            title: title,
            verticalButtons: true,
            buttons:buttons1
        });
    }

    //调往商品续期
    $('#gotoRenwal').click(function () {
        window.location.href = 'dancool://web='+urlView+'my/myEggPoolRenewal.html?type=0'
    });

    $('#batchHG').click(function () {
        window.location.href = 'dancool://web='+urlView+'my/batchHG.html?type=0'
    });

    $('#batchGM').click(function () {
        window.location.href = 'dancool://web='+urlView+'my/batchGM.html?type=0'
    });




    //调用app
    nativeCommand('dancool://titlebar=righttext|发货|nativeCommand("dancool://web='+urlView+'my/myEggPoolShipments.html?type=0")')
});
